import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../../../providers/ApiPro.service';
import { parts, HybSendOutGoodDto, HybSendOutGoodsDto, HybOrderDto } from '../HuoCustmp.model';
import { ActivatedRoute, Router, RouterLink, RouterModule } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';
import { DataService } from '../../../../providers/dataIO';




@Component({
  selector: 'app-HuoCusProDia',
  templateUrl: './HuoCusProDia.component.html',
  styleUrls: ['./HuoCusProDia.component.scss']
})
export class HuoCusProDiaComponent implements OnInit {
  public isDisabled = true;
  public _allChecked = false;
  public _indeterminate = false;
  public isVisible = false;
  public isConfirmLoading = false; 
  public radioValue = 1;
  public data = [];
  public dataPeople = [];
  public _total = 0;
  public _totalPeo = 0;
  public proInfo: HybOrderDto = new HybOrderDto();
  public pageIndex = 1;
  public parts: parts = new parts();
  public partsPeo: parts = new parts();
  public HybSendOutGoodsDto: HybSendOutGoodsDto = new HybSendOutGoodsDto();
  public hybSendOutGoodDto = [];
  
  public optionCar = [];
  constructor(
    public apiService: ApiService,
    private route: ActivatedRoute,
    private router: Router,
    private confirmServ: NzModalService,
    private dataIo: DataService

  ) { }

  ngOnInit() {
    // this.loadAllDriver();
    this.loadComs();
    this.getId();
  }

  getId() {
    this.route.params.subscribe((params) => {
      this.loadPro(params['id']);
    });
  }

  loadPro(id) {
    let url = "findsendoutgoodsinfo"
    this.apiService.get(url, { "orderId": id })
      .then(res => {
        this.proInfo = res.result;
        this.HybSendOutGoodsDto.orderId = this.proInfo.orderId;
      })
  }

  loadComs(){
    let url = "findlogisticscompanys";
    console.log(this.parts);
    this.apiService.get(url, this.parts)
      .then(res => {
        this._total = res.totalCount;
        this.data = res.result;
      })
  }

  checkDriver($event,i) {
    console.log($event);
    this.hybSendOutGoodDto[i].deposit = null;
    this.hybSendOutGoodDto[i].freight = null;
  }

  checkOpt(){
     for(let n = 0; n<this.optionCar.length ;n++){
       for( let m =0 ;m < this.hybSendOutGoodDto.length ;m++){
          //  let s = this.optionCar.indexOf(this.hybSendOutGoodDto[m]);
          // console.log(s);
          // if(s > 0){
          //   this.optionCar[s].disabled = true;
          // } else{
          //   this.optionCar[n].disabled = false;
          // }  
          if(this.optionCar[n].value==this.hybSendOutGoodDto[m].carId){
            this.optionCar[n].disabled = true;
            console.log(n);
            break;
          } else{
            this.optionCar[n].disabled = false;
            
          } 
       }
    } 
    
    console.log(this.hybSendOutGoodDto);
    console.log(this.optionCar);
  }

  //确定提交
  upData() {
    this.HybSendOutGoodsDto.hybSendOutGoodDto = [];
    for (let i = 0; i < this.dataPeople.length; i++) {
      let thisData = this.dataPeople[i];
      if (thisData.checked) {
        this.hybSendOutGoodDto[i].driverId = thisData.driverId;
        this.HybSendOutGoodsDto.hybSendOutGoodDto.push(this.hybSendOutGoodDto[i])
      }
    }
    console.log(this.HybSendOutGoodsDto);
    let url = "sendoutgoods";
    this.apiService.post(url, this.HybSendOutGoodsDto)
      .then(res => {
        console.log(res);
        if (res.result) {
          // this.router.navigateByUrl("layout/cus/pro");
          this.success();
        }else{
          this.warning(res.errMsg);
        }
      })
      .catch(err => {
        this.error(err.errMsg);
      })
  }

  error(err) {
    this.confirmServ.error({
      title: '错误',
      content: err,
      okText:'返回'
    });
  }

  success(){
    let self = this;
      this.confirmServ.success({
      title: '成功',
      content: '项目已产生派车单',
      okText:'查看派车单',
      cancelText:'返回项目管理',
      onOk() {
        self.router.navigateByUrl("layout/cus/car");
        self.dataIo.setNav("car");
      },
      onCancel() {
          // this.next();
          self.router.navigateByUrl("layout/cus/pro");
      }
    });
  }

  warning(war) {
    this.confirmServ.warning({
      title: '警告',
      content: war
    });
  }



  //表格全选方法
  _displayDataChange($event) {
    this.dataPeople = $event;
    this._refreshStatus();
  }

  _refreshStatus() {
    const allChecked = this.dataPeople.every(value => value.checked === true);
    const allUnChecked = this.dataPeople.every(value => !value.checked);
    this._allChecked = allChecked;
    this._indeterminate = (!allChecked) && (!allUnChecked);
  }

  _checkAll(value) {
    if (value) {
      this.dataPeople.forEach(data => {
        data.checked = true;
      });
    } else {
      this.dataPeople.forEach(data => {
        data.checked = false;
      });
    }
    this._refreshStatus();
  }

  //下一页界面
  resetIndex(e){
      console.log(e);
      this.parts.setPage(e);
      this.loadComs();
      // this._refreshStatus();
  }

  // 弹框
  loadAllDriver(e) {
    let url = "finddispatchedinfo";
    this.partsPeo.logisticsCompanyId = e;
    this.apiService.get(url, this.partsPeo)
      .then(res => {
        console.log(res);
        this._totalPeo = res.totalCount;
        this.dataPeople = res.result;
        for (let i = 0; i < res.totalCount; i++) {
          this.hybSendOutGoodDto[i] = new HybSendOutGoodDto();
        }
        console.log(this.hybSendOutGoodDto);
      })
      let urlc = "car/findcarbylogisticscompany";
      this.apiService.get(urlc,{"logisticsCompanyId":e})
        .then(res =>{
          this.optionCar = res.result;
          console.log(this.optionCar);
        this.isVisible = true;          
        })
  }


  showModal = (e) => {
    this.loadAllDriver(e);
  }

  handleOk = (e) => {
    this.isConfirmLoading = true;
    setTimeout(() => {
      this.isVisible = false;
      this.isConfirmLoading = false;
      this.upData();      
    }, 500);
  }

  handleCancel = (e) => {
    this.isVisible = false;
  }

}
